//
//  Bundle+Extension.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import Foundation

private class BundleToken {}

extension Bundle {
    
    private static func bundleWithName(_ bundleName: String) -> Bundle {

        let candidates = [
            // 所在app的包路径
            Bundle.main.resourceURL,

            // BundleToken 类， 所在组件framework的包路径（也就是当前组件的framework）
            Bundle(for: BundleToken.self).resourceURL,

            // 所在app的包路径，用于命令行工具（也是当前app的包路径）。
            Bundle.main.bundleURL,
        ]

        for candidate in candidates {
            let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
            if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
                return bundle
            }
        }

        return Bundle(for: BundleToken.self)
    }
    
    /// 获取当前组件中存放资源的bundle
    public static func zzFoundation() -> Bundle {
        return bundleWithName("ZZFoundation")
    }
      
}
